package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.Order;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.OrderService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping
    @LogScope(title = "订单管理模块", content = "查看订单列表信息")
    public Result select(@RequestParam Integer current) {

        log.info("查询第{}页的订单信息", current);
        // 分页查询 都返回PageInfo(分页插件工具)
        PageInfo<Order> orderPageInfo = orderService.selectOrderListByPage(current);
        return Result.success(orderPageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "订单管理模块", content = "根据id查询订单信息")
    public Result detail(@PathVariable String id){

        Order order = orderService.selectById(id);
        return Result.success(order);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "订单管理模块", content = "根据id删除订单信息")
    public Result delete(@PathVariable String id){

        int i = orderService.deleteById(id);
        return i>0 ? Result.success() : Result.error();
    }



    @PutMapping
    @LogScope(title = "订单管理模块", content = "编辑订单信息")
    public Result edit(@RequestBody Order order){

        int i = orderService.edit(order);
        return i>0 ? Result.success() : Result.error();
    }

    @PostMapping("/save")
    @LogScope(title = "用户订单管理模块", content = "添加用户订单信息")
    public Result save(@RequestBody Order order){

        order.setId(RandomUtil.getRandomNumber());
        order.setCreateTime(new Date());
        int i = orderService.save(order);
        return i>0 ? Result.success() : Result.error();
    }
    
}
